<?php
class UserFormValidation {

    public function cartFormValidation() {
        //TODO
    }

    public function cartModifyFormValidation() {
//TODO
    }

    public function orderFormValidation() {
        $CI =& get_instance();

        $CI->load->library('form_validation'); //코드이그나이터 검증 라이브러리
        //검증 규칙
        $config = array(array('field' => 'orderCode',
                             'label' => 'order code',
                             'rules' => 'trim|required|numeric|min_length[1]|max_length[10]|xss_clean'),
                        array('field' => 'orderPassword',
                             'label' => 'order password',
                             'rules' => 'trim|required|alpha_number_kr|min_length[1]|max_length[15]|xss_clean'),
                        array('field' => 'orderDate',
                             'label' => 'order date',
                             'rules' => 'trim|required|xss_clean'),
                        array('field' => 'buyerName',
                             'label' => 'buyer name',
                             'rules' => 'trim|required|alpha_number_kr|min_length[1]|max_length[20]|xss_clean'),
                        array('field' => 'buyerPhoneNumber01',
                             'label' => 'buyer phone number 01',
                             'rules' => 'trim|required|numeric|min_length[2]|max_length[4]|xss_clean'),
                        array('field' => 'buyerPhoneNumber02',
                             'label' => 'buyer phone number 02',
                             'rules' => 'trim|required|numeric|min_length[3]|max_length[4]|xss_clean'),
                        array('field' => 'buyerPhoneNumber03',
                             'label' => 'buyer phone number 03',
                             'rules' => 'trim|required|numeric|min_length[4]|max_length[4]|xss_clean'),
                        array('field' => 'buyerAddress',
                             'label' => 'buyer address',
                             'rules' => 'trim|required|alpha_number_kr|xss_clean'),
                        array('field' => 'billingSelect',
                             'label' => 'billing select',
                             'rules' => 'trim|required|alpha_kr|min_length[4]|max_length[4]|xss_clean')
        );

        $CI->form_validation->set_rules($config); //규칙대로 검증

        if($CI->form_validation->run() == true) { //검증 성공시
            if($CI->input->post('billingSelect', true) == 'card') { //결제수단이 카드일시
                //검증 규칙
                $config = array(array('field' => 'cardNumber01',
                                     'label' => 'card number 01',
                                     'rules' => 'trim|required|numeric|min_length[4]|max_length[4]|xss_clean'),
                                array('field' => 'cardNumber02',
                                     'label' => 'card number 02',
                                     'rules' => 'trim|required|numeric|min_length[4]|max_length[4]|xss_clean'),
                                array('field' => 'cardNumber03',
                                     'label' => 'card number 03',
                                     'rules' => 'trim|required|numeric|min_length[4]|max_length[4]|xss_clean'),
                                array('field' => 'cardNumber04',
                                     'label' => 'card number 04',
                                     'rules' => 'trim|required|numeric|min_length[4]|max_length[4]|xss_clean'));

                $CI->form_validation->set_rules($config); //규칙대로 검증

                if($CI->form_validation->run() == true) { //카드번호 검증 성공시
                    $Check = 'yes'; //결제확인 'yes'
                    $cardNumber = mysql_real_escape_string($CI->input->post('cardNumber01', true)).'-'.
                                 mysql_real_escape_string($CI->input->post('cardNumber02', true)).'-'.
                                 mysql_real_escape_string($CI->input->post('cardNumber03', true)).'-'.
                                 mysql_real_escape_string($CI->input->post('cardNumber04', true));
                } else {
                    return false;
                }
            } else { //결제수단이 현금일시
                //검증 규칙
                $config = array(array('field' => 'cardNumber01',
                                     'label' => 'card number 01',
                                     'rules' => 'trim|blank'),
                                array('field' => 'cardNumber02',
                                     'label' => 'card number 02',
                                     'rules' => 'trim|blank'),
                                array('field' => 'cardNumber03',
                                     'label' => 'card number 03',
                                     'rules' => 'trim|blank'),
                                array('field' => 'cardNumber04',
                                     'label' => 'card number 04',
                                     'rules' => 'trim|blank'));

                $CI->form_validation->set_rules($config); //규칙대로 검증

                if($CI->form_validation->run() == true) { //검증 성공시
                    $Check = 'no'; //결제확인 'no'
                } else {
                    return false;
                }
            }

            $result = $CI->userlibrary->orderInsert($Check); //주문 입력 라이브러리
            return $result;
        } else
            return false;
    }
}
